package com.work.washer.service;

import com.baomidou.mybatisplus.extension.service.IService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.work.washer.dto.RepairReq;
import com.work.washer.mapper.RepairMapper;
import com.work.washer.model.Comment;
import com.work.washer.model.Machine;
import com.work.washer.model.Repair;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @author
 * @description 针对表【type】的数据库操作Service实现
 * @createDate 2024-02-20 10:07:14
 */
@Service
public class RepairService extends ServiceImpl<RepairMapper, Repair>
        implements IService<Repair> {

    @Autowired
    private MachineService machineService;

    @Autowired
    private CommentService commentService;

    public Boolean sendFail(Repair repair) {
        Machine machine = machineService.getById(repair.getMachineId());
        if (machine.getState() == 2) {
            throw new RuntimeException("设备维修中");
        }
        repair.setCreateTime(new Date());
        repair.setStatus(1);
        this.save(repair);
        machine.setState(2);
        machineService.updateById(machine);
        return true;
    }

    public List<Repair> getFail(Integer type, Integer userId, Integer status) {
        if (type == 1) {
            List<Repair> list = this.lambdaQuery().orderByDesc(Repair::getId).list();
            if (status != null) {
                list = this.lambdaQuery().eq(Repair::getStatus, status).orderByDesc(Repair::getId).list();
            }
            list.forEach(e -> {
                Machine machine = machineService.getById(e.getMachineId());
                e.setMachine(machine);
            });
            return list;
        }
        if (type == 2) {
            List<Repair> list = this.lambdaQuery().eq(Repair::getUserId, userId).orderByDesc(Repair::getId).list();
            list.forEach(e -> {
                Machine machine = machineService.getById(e.getMachineId());
                e.setMachine(machine);
            });
            return list;
        }
        return null;
    }

    public Boolean repair(RepairReq repairReq) {
        Machine machine = machineService.getById(repairReq.getMachineId());
        if (machine.getState() == 1) {
            return true;
        }
        machine.setState(1);
        machineService.updateById(machine);
        this.lambdaUpdate().eq(Repair::getMachineId, repairReq.getMachineId())
                .set(Repair::getStatus,2)
                .set(Repair::getSysUserId,repairReq.getSysUserId())
                .set(Repair::getSysUserRemark,repairReq.getSysRemark())
                .set(Repair::getImage,repairReq.getImage()).update();
        return true;
    }

    public boolean addComment(Integer repairId, Integer userId, String content,String image) {
        Comment comment = new Comment();
        comment.setRepairId(repairId);
        comment.setUserId(userId);
        comment.setCreateDate(new Date());
        comment.setContent(content);
        comment.setImage(image);
        commentService.save(comment);
        return true;
    }

    public List<Comment> getComment(Integer repairId) {
        return commentService.getByRepairId(repairId);
    }
}




